<template>
  <div class="cates-container">
    <div class="left">
      <van-sidebar v-model="activeKey" @change="handleChange">
        <van-sidebar-item :title="item1.cat_name" v-for="item1 in cateList" :key="item1.cat_id" />
      </van-sidebar>
    </div>
    <div class="right">
      <div v-for="item2 in secondCateList" :key="item2.cat_id">
        <van-row class="cat_name">
          <van-col span="24">
            <span>{{'/' + item2.cat_name + '/' }}</span>
          </van-col>
        </van-row>
        <van-row>
          <van-col>
            <van-grid :column-num="3" :border="false" clickable>
              <van-grid-item
                v-for="item3 in item2.children"
                :key="item3.cat_id"
                @click="goodsList(item3.cat_id)"
              >
                <van-image :src="item3.cat_icon" />
                <div class="thirdCatName">{{item3.cat_name}}</div>
              </van-grid-item>
            </van-grid>
          </van-col>
        </van-row>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 商品分类数据
      cateList: [],
      activeKey: 0,
      // 二级分类数据
      secondCateList: []
    };
  },
  methods: {
    async getCateList() {
      const { data } = await this.$http.get("/categories");
      //   console.log(data);
      if (data.meta.status != 200) return this.$toast.fail("数据加载失败");
      this.cateList = data.message;
    },
    // 侧边栏切换触发
    handleChange() {
      this.secondCateList = this.cateList[this.activeKey].children;
    },
    goodsList(cid) {
      this.$router.push("/pages/goods_list?cid=" + cid);
    }
  },
  async created() {
    await this.getCateList();
    this.secondCateList = this.cateList[this.activeKey].children;
  }
};
</script>

<style lang="less" scoped>
.cates-container {
  display: flex;
  height: 575px;
}
.left {
  overflow-y: auto;
  width: 85px;
}
.right {
  flex: 1;
  overflow-y: auto;
  .cat_name {
    text-align: center;
    font-weight: bold;
    height: 60px;
    line-height: 60px;
  }
  .van-image {
    width: 60px;
    height: 60px;
  }
  .thirdCatName {
    font-size: 12px;
    margin-top: 10px;
  }
}
</style>